﻿using Padsurface.App.Services.Interfaces;
using System.Collections.Generic;
using System.Composition;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;

namespace Padsurface.App.Services
{
    [Export(typeof(IFilePicker))]
    internal class FilePicker : IFilePicker
    {
        private const string WaveFileExtension = ".wav";
        private const string JsonFileExtension = ".json";

        public IAsyncOperation<StorageFile> OpenSingleWaveFileAsync()
        {
            var filePicker = CreateFileOpenPicker(WaveFileExtension);

            return filePicker.PickSingleFileAsync();
        }

        public IAsyncOperation<IReadOnlyList<StorageFile>> OpenMultipleWaveFilesAsync()
        {
            var filePicker = CreateFileOpenPicker(WaveFileExtension);

            return filePicker.PickMultipleFilesAsync();
        }

        public IAsyncOperation<StorageFile> SaveWaveFileAsync(string suggestedFileName = null)
        {
            var filePicker = CreateWaveFileSavePicker(suggestedFileName);

            return filePicker.PickSaveFileAsync();
        }

        public IAsyncOperation<StorageFile> OpenSingleSongFileAsync()
        {
            var filePicker = CreateFileOpenPicker(JsonFileExtension);

            return filePicker.PickSingleFileAsync();
        }

        public IAsyncOperation<StorageFile> SaveSongFileAsync(string suggestedFileName = null)
        {
            var filePicker = CreateSongFileSavePicker(suggestedFileName);

            return filePicker.PickSaveFileAsync();
        }

        private FileOpenPicker CreateFileOpenPicker(string fileType)
        {
            var fileOpenPicker = new FileOpenPicker
            {
                SuggestedStartLocation = PickerLocationId.MusicLibrary
            };

            fileOpenPicker.FileTypeFilter.Add(fileType);

            return fileOpenPicker;
        }

        private FileSavePicker CreateWaveFileSavePicker(string suggestedFileName = null)
        {
            var fileSavePicker = new FileSavePicker
            {
                SuggestedStartLocation = PickerLocationId.MusicLibrary,
                DefaultFileExtension = WaveFileExtension,
            };

            if (!string.IsNullOrWhiteSpace(suggestedFileName))
                fileSavePicker.SuggestedFileName = suggestedFileName;

            fileSavePicker.FileTypeChoices.Add("WAVE", new List<string>() { WaveFileExtension });

            return fileSavePicker;
        }

        private FileSavePicker CreateSongFileSavePicker(string suggestedFileName = null)
        {
            var fileSavePicker = new FileSavePicker
            {
                SuggestedStartLocation = PickerLocationId.MusicLibrary,
                DefaultFileExtension = JsonFileExtension,
            };

            if (!string.IsNullOrWhiteSpace(suggestedFileName))
                fileSavePicker.SuggestedFileName = suggestedFileName;

            fileSavePicker.FileTypeChoices.Add("JSON", new List<string>() { JsonFileExtension });

            return fileSavePicker;
        }
    }
}